<?php  //UTF-8
  if($step != 4)
    die();
    
  include_once('./includes/funcs_wow.php');
?>

<!-- STYLES -->
  <style type="text/css">
    .form table {
      /*width: 100%;*/
      margin: 10px auto 10px auto;
      border-spacing: 8px 0px;
    }
    
    table tr td {
      text-align: left;
    }
    
    .error {
      text-align: center;
      color: red;
      font-size: 15px;
    }
    
    .finish {
      text-align: center;
      color: blue;
      font-size: 18px;
    }
    
    .finish_data {
      padding-top: 20px;
    }
  </style>

<!-- SCRIPTS -->

<!-- BODY -->
<?php

  $err_str = '';

/* Steps data checking */  
 /* Step 2 */
  if((get_usr_id(addslashes($_POST['value_1'])) === false) && ($_POST['sign_type'] != 0)) {
    $err_str .= 'Character with entered name does not exists and signature can\'t be generated.<br>';
    $stop = true;
  }

 /* Step 1 */
  if(($_POST['sign_type'] < 0) || ($_POST['sign_type'] > 2)) {
    $err_str .= 'Type of signature is invalid.<br>';
    $stop = true;
  }
 
  $sql_return = @mysql_query('SELECT * FROM `'.TAB_SRC.'` WHERE `id`='.intval($_POST['background']).' AND `type`="SIGNATURE" LIMIT 1');
  $sql_data = @mysql_fetch_assoc($sql_return);
  if($sql_data['id'] == null) {
    $err_str .= 'Choosen signature background does not exists.<br>';
    $stop = true;
  }

 /* Step 2 */  
  if(($_POST['num_texts'] > $sql_data['fields']) || ($_POST['num_texts'] < 1)) {
    $err_str .= 'Wrong number of texts on signature.<br>';
  }
  
  if($_POST['sign_type'] == 0) {
    for($i=1; $i<=intval($_POST['num_texts']); $i++) {
      if(strlen($_POST['value_'.$i]) > 50) {
        $err_str .= 'Text value in field '.$i.' is too long.<br>';
        $stop = true;
      } else {
        $funcs[$i] = 0;
        $vals[$i] = strtr($_POST['value_'.$i], '|"\'', '   ');
      }
    }
  } else {
    $options = count(get_values(PATCH));
    for($i=1; $i<=intval($_POST['num_texts']); $i++) {
      if((((int)$_POST['func_'.$i] < 0) || ((int)$_POST['func_'.$i] > $options)) && ($i != 1)) {
        $err_str .= 'Found wrong auto value in field '.$i.'<br>';
        $stop = true;
      } else {
        $funcs[$i] = intval($_POST['func_'.$i]);
      }
      
      if(strlen($_POST['value_'.$i]) > 50) {
        $err_str .= 'Text value in field '.$i.' is too long.<br>';
        $stop = true;
      } else {
        $vals[$i] = strtr($_POST['value_'.$i], '|"\'', '   ');
      }
    }
  }
  
 /* Step 3 */
  for($i=1; $i<=intval($_POST['num_texts']); $i++) {
    $sql_ret = @mysql_query('SELECT * FROM `'.TAB_SRC.'` WHERE `id`='.intval($_POST['f'.$i.'_font']).' AND `type`="FONT" LIMIT 1');
    $sql_dat = @mysql_fetch_assoc($sql_ret);
    if($sql_dat['id'] == null) {
      $err_str .= 'Font selected in field '.$i.' does not exists.<br>';
      $stop = true;
    }
    
    if(!ereg('^[a-fA-F0-9]{6}$', $_POST['f'.$i.'_color'])) {
      $err_str .= 'Color in field '.$i.' is invalid.<br>';
      $stop = true;
    }
    
    if(!strcasecmp($_POST['f'.$i.'_shadow'], 'on')) {
      $shadows[$i] = 1;
      if(!ereg('^[a-fA-F0-9]{6}$', $_POST['f'.$i.'_color_shadow'])) {
        $err_str .= 'Shadow color in field '.$i.' is invalid.<br>';
        $stop = true;
      } else {
        $sh_cols[$i] = $_POST['f'.$i.'_color_shadow'];
      }
    } else {
      $shadows[$i] = 0;
      $sh_cols[$i] = ' ';
    }
    
  }
?>
    <table>

<?php

  if($stop) {
    echo('
      <tr>
        <td>
          <b>While genetating script encountered the following errors:</b>
        </td>
      </tr>
      <tr>
        <td class="error">
          '.$err_str.'
        </td>
      </tr>
    ');
  } else {
    /* generating strings */
    for($i=1; $i<=intval($_POST['num_texts']); $i++) {
      $f_str[$i] = $funcs[$i].'|'.$vals[$i].'|'.intval($_POST['f'.$i.'_font']).'|'.$_POST['f'.$i.'_color'].'|'.$shadows[$i].'|'.$sh_cols[$i].'|'.intval($_POST['f'.$i.'_sh_movex']).'|'.intval($_POST['f'.$i.'_sh_movey']);
    }
    for($i=(intval($_POST['num_texts'])+1); $i<=9; $i++) {
      $f_str[$i] = ' ';
    }
    mysql_query('INSERT INTO `'.TAB_DAT.'` VALUES(null, '.intval($_POST['sign_type']).', '.intval($_POST['background']).', '.intval($_POST['num_texts']).', "'.$f_str[1].'", "'.$f_str[2].'", "'.$f_str[3].'", "'.$f_str[4].'", "'.$f_str[5].'", "'.$f_str[6].'", "'.$f_str[7].'", "'.$f_str[8].'", "'.$f_str[9].'") ');
    
    $guid = mysql_insert_id($conn);
    $link = LINK.'/sign.php?id='.$guid;
    
    echo('
      <tr>
        <td class="finish">
          Congratulations !!!<br>
          You generated your own signature.<br><br>
          
          <img src="./sign.php?id='.$guid.'" title="'.$vals[1].'\'s signature" alt="Signature"><br><br>
        </td>
      </tr>
      
      <tr>
        <td class="finish_data">
          <b>Image Link:</b> <br>
          <textarea rows="2" cols="80" onclick="this.select()">'.$link.'</textarea>
        </td>
      </tr>
      <tr>
        <td class="finish_data">
          <b>HTML Code:</b> <br>
          <textarea rows="2" cols="80" onclick="this.select()"><a href="http://'.$_SERVER['HTTP_HOST'].'/"><img src="'.$link.'" title="'.$vals[1].'\'s signature" alt="Signature"></a></textarea>
        </td>
      </tr>
      <tr>
        <td class="finish_data">
          <b>BBCODE</b> <br>
          <textarea rows="2" cols="80" onclick="this.select()">[url=http://'.$_SERVER['HTTP_HOST'].'/][img]'.$link.'[/img][/url]</textarea>
        </td>
      </tr>
    ');
  }
?>
    </table>